home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-11 | 2.6 KB | 59 lines | [TEXT/MMCC] |
- // View this file in Geneva 9 with 4 space tabs.
- // This code was written by François Pottier (pottier@dmi.ens.fr) and compiled with CodeWarrior 4.5. You can use it freely.
-
- #pragma once
-
- /*
- This class lets the user operate directly and transparently on Pascal strings. C strings are evil, and dealing with Pascal strings
- all the time is cooler. Of course, that's just my point of view.
- The class is designed so that you can use a PascalString anywhere instead of a Str255, transparently. PascalStrings have the
- cool + and += operators, too.
-
- The PascalString class has automatic conversions to and from Str255 ( = StringPtr), OSType and long.
- It also has string copy and string concatenation.
- The class has a simple design ; it is not meant to be very efficient. It could be easily sped up and extended, but I think it's a
- starting point to show a few interesting aspects of C++.
- */
-
- class PascalString {
- Str255 S;
- public:
- PascalString (); // Default constructor
- PascalString (const Str255& source); // Conversion Str255 -> class
- PascalString (OSType source); // Conversion OSType -> class
- PascalString (long source); // Conversion long -> class
- PascalString (const PascalString& source); // Initialization with copy
-
- operator StringPtr () { return S; }; // Conversion class -> Str255
- operator OSType (); // Conversion class -> OSType
- operator long (); // Conversion class -> long
-
- PascalString& operator= (const PascalString& source); // Assignment
- PascalString& operator= (const Str255& source); // Assignment from Str255
- PascalString& operator+= (const PascalString& source); // Concatenation
- PascalString& operator+= (const Str255& source); // Concatenation from Str255
-
- friend PascalString operator+ (const PascalString&, const PascalString&);
- };
-
- // As a bonus, here a few string utilities.
-
- void StringCopy (const unsigned char *source, StringPtr dest); // Copies a Pascal string into another
- void String2Text (const unsigned char *source, StringPtr dest); // Copies a Pascal string into a raw text buffer
- short PStringCmp (StringPtr S1, StringPtr S2); // Compares two Pascal strings
-
- // Useful macros to copy strings. Use them if you only have a few string copies to do and don't want to use the PascalString class
- // for such a little job.
-
- typedef struct { // Kluge to create efficient code for PStrCopy31
- Str31 S;
- } Dummy31Rec, *Dummy31Ptr;
-
- #define PStrCopy31(S, T) * (Dummy31Ptr) (T) = * (Dummy31Ptr) (S)
-
- typedef struct {
- Str255 S;
- } Dummy255Rec, *Dummy255Ptr;
-
- #define PStrCopy255(S, T) * (Dummy255Ptr) (T) = * (Dummy255Ptr) (S)
-